use "`base'\Data Files and Code That Produced Them\P09 Inpatient Analysis File", replace


*These coefficients come from "P11 Estimate population for weights.sas" and are the slope of a regression of population on age in years. So convert age in months in this file
*into years then multiply by slope to get weights. Add one so it is effectively proportion and deals with 0 issue. Stata will do the rescaling.  
gen pop_all_np =  1 - 0.00926*(months_21/12)
gen pop_m      =  1 - 0.01296*(months_21/12)
gen pop_f_np   =  1 - 0.00537*(months_21/12)

local dep_var "visit illness injury_or_alc alcohol_any inj_accident  inj_by_self inj_by_oth"
 
label variable over "Over 21"
local suffs "all_np m f_np"
local top_title "Table 3: Inpatient Visits Visits"

local replace "replace"
foreach suf of local suffs {
   foreach y of local dep_var {
      local col_head1 = ""
      local col_head2 = ""
      local col_head3 = ""
      if `y'_`suf'_r == visit_`suf'_r  local col_head1 = "All Visits" 
      if `y'_`suf'_r == illness_`suf'_r local col_head1 = "Illness"
      if `y'_`suf'_r == injury_or_alc_`suf'_r local col_head1 = "Injury or "
      if `y'_`suf'_r == injury_or_alc_`suf'_r local col_head2 = "Alcohol"
      if `y'_`suf'_r == alcohol_any_`suf'_r local col_head1 = "Alcohol"
      if `y'_`suf'_r == inj_accident_`suf'_r local col_head1 = "Accidental"
      if `y'_`suf'_r == inj_accident_`suf'_r local col_head2 = "Injury"
      if `y'_`suf'_r == inj_by_self_`suf'_r local col_head1 = "Self"  
      if `y'_`suf'_r == inj_by_self_`suf'_r local col_head2 = "Inflicted"  
      if `y'_`suf'_r == inj_by_self_`suf'_r local col_head3 = "Injury"   
      if `y'_`suf'_r == inj_by_oth_`suf'_r local col_head1 = "Injury "
      if `y'_`suf'_r == inj_by_oth_`suf'_r local col_head2 = "Inflicted"
      if `y'_`suf'_r == inj_by_oth_`suf'_r local col_head3 = "by Other"
      reg `y'_`suf'_r  over dummy21   age_c age_c_sq age_c_post age_c_post_sq [aweight=pop_`suf'] if months_21 >= -24 & months_21 <= 23, robust 
      outreg2  using "Table 3 Raw.xls",  sdec(1)  bdec(1)  keep(over) br noaster  `replace'   title("`top_title' `suf'") label ctitle("`col_head1',`col_head2',`col_head3'")  
      local replace "append"
	  rdbwselect `y'_`suf'_r age_c , kernel(uniform)  p(2)  bwselect(IK)
      rdbwselect `y'_`suf'_r age_c , kernel(uniform)  p(2)  bwselect(CCT)
   }
}
 
 *Move the file using the dos prompt - there appears to be an issue with the long path when using outreg2
!move /Y "Table 3 Raw.xls" "`base'\Code for Figures and Tables in Paper\"

*To get p-value of differences between men and women
local m_f "m f_np"
local dep_var "visit illness injury_or_alc alcohol_any inj_accident  inj_by_self inj_by_oth"
*Create a dataset of men;
foreach suf of local m_f {
   use "C:\Research\Alcohol and Morbidity\4. Code up from scratch\P09 Inpatient Analysis File", replace
   keep months_21 over dummy21   age_c age_c_sq age_c_post age_c_post_sq visit_`suf'_r illness_`suf'_r injury_or_alc_`suf'_r alcohol_any_`suf'_r inj_accident_`suf'_r  inj_by_self_`suf'_r inj_by_oth_`suf'_r
   foreach y of local dep_var {
      *Strip the suffix
      gen `y'_r = `y'_`suf'_r
	  drop `y'_`suf'_r
   }  
   gen male = 0
   replace male = 1 if "`suf'" == "m"
   
  *Create the weights for men and women the age profile is declining so slope is negative
   gen     pop_weight   =  1 - 0.00537*(months_21/12)
   replace pop_weight   =  1 - 0.01296*(months_21/12) if "`suf'" == "m"
   
   save temp_`suf' , replace
}   

use temp_m, replace
append using temp_f_np

*Create female gender specific age profile
local age_vars "over dummy21 age_c age_c_sq age_c_post age_c_post_sq"
foreach x of local age_vars {
   gen `x'_f = 0
   replace `x'_f = `x' if male == 0 
   gen `x'_m = 0
   replace `x'_m = `x' if male == 1 
}

keep if months_21 >= -24 & months_21 <= 23
gen constant_f = 0
replace constant_f = 1 if male == 0

*Regressions match including SE - get p-value of gender difference from this
foreach y of local dep_var {
	  reg `y'_r over_m dummy21_m age_c_m age_c_sq_m age_c_post_m age_c_post_sq_m constant_f over_f dummy21_f age_c_f age_c_sq_f age_c_post_f age_c_post_sq_f [aweight = pop_weight] if months_21 >= -24 & months_21 <= 23, robust
      test over_m = over_f	 
}

 
